<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = sxREQ('msg');
$process_unit = sxREQ('pu');
$entry_point = sxREQ('ep');
if($entry_point == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'ep'"));
	serverRedir($url);
}
if($process_unit == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'pu'"));
	serverRedir($url);
}
$sSQL = "SELECT * FROM entry_point " .
		"WHERE process_unit='".sxRAS('pu')."' " .
		"AND entry_point=".sxRInt("ep")."";
$objSDAO->query($sSQL,"selep");
$rowPU = $objSDAO->fetch("selep");
if(!$rowPU){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing  entry point $process_unit/$entry_point"));
	serverRedir($url);
}
if(sxREQ("reqtype") == "enable"){
	$errMsg .= entpAliasEnable($process_unit, $entry_point, sxREQ('al'), 'N');
}elseif(sxREQ("reqtype") == "enableRO"){
	$errMsg .= entpAliasEnable($process_unit, $entry_point, sxREQ('al'), 'Y');
}elseif(sxREQ("reqtype") == "disnable"){
	$errMsg .= entpAliasDisabilita($process_unit, $entry_point, sxREQ('al'));
}elseif(sxREQ("reqtype") == "delete"){
	$errMsg .= entpDel();
}
$_SESSION[USEROBJ]->setHistoryVar(array('reqtype'=>'','alias'=>''));

$sSQL = "SELECT pua.*, epa.read_only AS epa_read_only , e.description AS ent_des FROM pu_alias pua " .
			" LEFT JOIN entry_point_alias epa ON pua.process_unit=epa.process_unit AND pua.alias=epa.alias " .
			"    AND epa.entry_point='$entry_point' ".
			" LEFT JOIN entity e ON e.entity = pua.entity ".
			" WHERE pua.process_unit='$process_unit'";
$objSDAO->query($sSQL, "selAl");

$sSQL = "SELECT * FROM entry_point_rule "
	." WHERE entry_point=$entry_point AND process_unit='$process_unit' "
	." ORDER BY process_unit, entry_point, name";
$errMsg = "";
$query = $objSDAO->query($sSQL, "selRule");

$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function confDel() {
	if (confirm("Confirm ?")) {
		document.location.href = "entpView.php?<?="reqtype=delete&pu=$process_unit&ep=$entry_point&xsn=".XSN?>";
	}	
}
</script>
<?php
echo getPuHeader($process_unit);

$aCmd = array("del"=>array("pag"=>"javascript:confDelEntryPoint();",
										"title"=>_t("delete"),
										"text"=>_t("delete")
										),
					"mod"=>array("pag"=>$pref."entpMod.php?pu=$process_unit&ep=$entry_point",
										"title"=>_t("change"),
										"text"=>_t("change")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Entry point</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Code</td>" .
			"<td class=\"text01\">$entry_point</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">".sxHE($rowPU['description'])."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">User range</td>" .
			"<td class=\"text01\">".sxHE($rowPU['user_range'])."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($row['note']))."</pre></td>" .
		"</tr>\n");
echo("</table>");
//aliases list
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
		"<td class=\"titl01\" colspan=\"20\">Alias enabled</td>" .
	"</tr>\n");
echo("<tr>" .
		"<td class=\"tabh01\" width=\"25%\" align='center'>Change</td>" .
		"<td class=\"tabh01\" width=\"10%\" align='center'>Enabled/Disabled</td>" .
		"<td class=\"tabh01\" width=\"1%\" align='center'>Alias</td>" .
		"<td class=\"tabh01\" width=\"60%\" align='center'>Entity</td>" .
	"</tr>\n");	
while($row = $objSDAO->fetch("selAl")){
	$href = "entpView.php?pu=$process_unit&ep=$entry_point&xsn=".XSN."&al=".$row["alias"];
	echo("<tr>");
	if ($row['epa_read_only'] == '') {
		if ($row['read_only'] == 'N') {
			echo("<td class=\"text01\" valign='top' align='center'>"
				."<a href='$href&reqtype=enable' class='text01'>Enable</a>"
				."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				."<a href='$href&reqtype=enableRO' class='text01'>Read only</a></td>\n");
		} else {
			echo("<td class=\"text01\" valign='top' align='center'>"
				."<a href='$href&reqtype=enableRO' class='text01'>Read only</a></td>\n");
		}
	} elseif ($row['epa_read_only'] == 'N') {
			echo("<td class=\"text01\" valign='top' align='center'>"
				."<a href='$href&reqtype=disable' class='text01'>Disable</a>"
				."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				."<a href='$href&reqtype=enableRO' class='text01'>Read-only</a></td>\n");
	} else { // epa_read_only == 'Y'
		if ($row['read_only'] == 'N') {
			echo("<td class=\"text01\" valign='top' align='center'>"
				."<a href='$href&reqtype=disable' class='text01'>Disable</a>"
				."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				."<a href='$href&reqtype=enable' class='text01'>Enable</a></td>\n");
		} else {
			echo("<td class=\"text01\" valign='top' align='center'>"
				."<a href='$href&reqtype=diable' class='text01'>Disable</a></td>\n");
		}
	}
	$style = ($row["epa_read_only"]=='')?" style='color:#888888;font-style:italics;'":"";
	echo ("<td class=\"text01\" valign='top'$style align='center'>".
			(($row["epa_read_only"]=='')?'disabled':(($row["epa_read_only"]=='Y')?'read-only':'enabled'))."</td>");
	echo("<td class=\"text01\" valign='top'$style align='center'>".
				$row["alias"]."</td>\n");
	echo ("<td class=\"text01\" valign='top'$style>".$row["entity"]." - ".$row["ent_des"]."</td>");
	echo ("</tr>\n");
}
//Rules list
echo("</table>\n");
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmdRule = array("pag"=>"entpRuleIns.php?pu=$process_unit&ep=$entry_point",
						"title"=>_t("add new rule"),
						"text"=>_t("new rule"),
						"perm"=>"viewmod");
echo getTitleLink('Rules', $aCmdRule);
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\">Seq.</td>" .
		"<td class=\"tabh01\" width=\"10%\">Rule</td>" .
		"<td class=\"tabh01\" width=\"40%\">Description</td>" .
		"<td class=\"tabh01\" width=\"15%\">Subroutine</td>" .
		"<td class=\"tabh01\" width=\"34%\">Common rule</td>" .
	"</tr>\n");	
while($row = $objSDAO->fetch("selRule")){
	echo("<tr>" .
				"<td class=\"text01\" valign='top' align='right'>".$row["sequence"]."</td>" .					
				"<td class=\"text01\" valign='top'><a class=\"text01\" ".
				goToPag($pref."entpRuleView.php?pu=$process_unit&ep=$entry_point&name=".$row["name"]."&xsn=".XSN).
				" title=\""._t("visualizza la $pageSubject ")."'".$row["name"]."'\">".
				$row["name"]."</a></td>\n" .
			"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
			"<td class=\"text01\" valign='top' align='center'>".(($row["is_sub"]=='Y')?'yes':'no')."</td>" .
			"<td class=\"text01\" valign='top'>".$row["com_rule_name"]."</td>" .
		"</tr>\n");
}
echo("</table>\n");
echo $sCmd;
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function entpAliasEnable($process_unit, $entry_point, $alias, $read_only){
	global $objSDAO;
	$errMsg = "";
	//check existence
	$sSQL = "SELECT entry_point as num FROM entry_point_alias " .
			"WHERE process_unit='$process_unit' " .
			"AND entry_point = $entry_point " .
			"AND alias='$alias'";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if(!$row) {
		$sSQL = "INSERT INTO " .
				"entry_point_alias " .
				"(process_unit, " .
				"entry_point, " .
				"alias, " .
				"read_only)" .
				"VALUES('$process_unit', " .
				"$entry_point, " .
				"'$alias', " .
				"'$read_only')";	
	} else {
		$sSQL = "UPDATE entry_point_alias SET " .
			"read_only='$read_only' " .
			"WHERE process_unit='$process_unit' AND " .
			"entry_point=$entry_point AND " .
			"alias='$alias'";	
	}			
	$objSDAO->query($sSQL,"epa");
	return '';
}

function entpAliasDisable($process_unit, $entry_point, $alias){
	global $objSDAO;
	$errMsg = "";
	$sSQL = "DELETE FROM entry_point_alias " .
				"WHERE process_unit='$process_unit' " .
					" AND entry_point=$entry_point AND alias='$alias'";	
	$objSDAO->query($sSQL,"epa");
	return '';
}

function entpDel(){
	global $objSDAO;
	$errMsg = "";
	//check rules existence
	$sSQL = "SELECT name as num FROM entry_point_rule " .
			"WHERE process_unit='".sxRAS("pu")."' " .
			"AND entry_point=".sxRAS("ep")."";
	$objSDAO->query($sSQL,"del");
	$row = $objSDAO->fetch("del");
	if($row) {
		$errMsg .= "existing rules<br>";
	}
	if ($errMsg != '') {
		return $errMsg;
	}
	
	$sSQL = "DELETE FROM entry_point_alias " .
			"WHERE process_unit='".sxRAS("pu")."' AND " .
			"entry_point='".sxRAS("ep")."'";		
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM entry_point_attribute " .
			"WHERE process_unit='".sxRAS("pu")."' AND " .
			"entry_point='".sxRAS("ep")."'";		
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM entry_point_variable " .
			"WHERE process_unit='".sxRAS("pu")."' AND " .
			"entry_point='".sxRAS("ep")."'";		
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM entry_point " .
			"WHERE process_unit='".sxRAS("pu")."' AND " .
			"entry_point='".sxRAS("ep")."'";		
	$objSDAO->query($sSQL,"del");
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'redir err';
}